package com.gomore.opple.module.main.mine;

import android.text.TextUtils;

import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.applybill.ApplyRequest;
import com.gomore.opple.rest.applybill.EarningsResponse;
import com.gomore.opple.rest.employee.LoginResult;
import com.gomore.opple.rest.employee.ModifyPasswordRequest;
import com.gomore.opple.rest.employee.RsEmployeeRequest;
import com.gomore.opple.web.cgform.applybill.entity.TOApplyBillEntity;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.system.pojo.TOEmployee;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by asus on 2016/12/27.
 */
public class MinePresenter implements MineContract.Presenter {
    private DataRepository mDataRepositroy;
    private final MineContract.View mView;
    private CompositeSubscription mSubscriptions;
    private EarningsResponse earningsResponses;

    @Inject
    MinePresenter(DataRepository dataRepository, MineContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }

    @Override
    public void prepareInitData() {
        String url = "demo";
        if (mDataRepositroy.getUser() == null) {
            return;
        }
        TOEmployee toEmployee = mDataRepositroy.getUser().getEmployee();
        if (toEmployee == null) {
            return;
        }
        if (toEmployee.getPicture() != null) {
            url = toEmployee.getPicture();
        }
        mView.showIcon(url);
    }

    @Override
    public void getTotal() {
        unsubscribe();
        mView.showProgressDialog();
        String employeeId = mDataRepositroy.getUser().getEmployee().getId();
        Subscription subscription = mDataRepositroy.getTotal(employeeId)
                .map(new ServerResponseFunc<EarningsResponse>())
                .onErrorResumeNext(new HttpResponseFunc<EarningsResponse>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<EarningsResponse>() {
                    @Override
                    public void onNext(EarningsResponse earningsResponse) {
                        super.onNext(earningsResponse);
                        mView.hideProgressDialog();
                        earningsResponses = earningsResponse;
                        mView.showContentView(earningsResponse);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public EarningsResponse getData() {
        return earningsResponses;
    }

    @Override
    public void deleteBankCard(String bankNumber) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.unbind(bankNumber)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage("银行卡解绑成功");
                        getTotal();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void applySave(ApplyRequest applyRequest) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.applySave(applyRequest)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        if (!TextUtils.isEmpty(string)) {
                            mView.showApplyAfaterDialog(string);
                        }
                        getTotal();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public TOConsumerEntity getConsumer() {
        return mDataRepositroy.getConsumer();
    }

    @Override
    public void logout() {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.logout()
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        //清除登陆信息
                        mDataRepositroy.clearMessage();
                        mView.gotoLoginActivity();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public LoginResult getUser() {
        return mDataRepositroy.getUser();
    }

    @Override
    public void modifyPassword(String oldPassword, String newPassword) {
        ModifyPasswordRequest modifyPasswordRequest = new ModifyPasswordRequest();
        modifyPasswordRequest.setOldPassword(oldPassword);
        modifyPasswordRequest.setNewPassword(newPassword);
        modifyPasswordRequest.setEmployeeId(mDataRepositroy.getUser().getEmployee().getId());
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.modifyPassword(modifyPasswordRequest)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.gotoLoginActivity();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void saveAttachment(MultipartBody.Part file) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.uploadAttachment(file)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String url) {
                        super.onNext(url);
                        mView.hideProgressDialog();
                        saveEmployee(url);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.getMessage());
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void saveEmployee(String url) {
        RsEmployeeRequest rsEmployeeRequest = new RsEmployeeRequest();
        TOEmployee toEmployee = mDataRepositroy.getUser().getEmployee();
        toEmployee.setPicture(url);
        rsEmployeeRequest.setEmployee(toEmployee);
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.updateEmployee(rsEmployeeRequest)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage("头像保存成功");
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void getDetailState(String uuid) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.getDetailState(uuid)
                .map(new ServerResponseFunc<TOApplyBillEntity>())
                .onErrorResumeNext(new HttpResponseFunc<TOApplyBillEntity>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<TOApplyBillEntity>() {
                    @Override
                    public void onNext(TOApplyBillEntity toApplyBillEntity) {
                        super.onNext(toApplyBillEntity);
                        mView.hideProgressDialog();
                        mView.goToApplyDetail(toApplyBillEntity);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }


    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }
}
